﻿using System;
using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing;
using System.Windows.Forms;

namespace XFSenseUI.XFItems.SenseControls
{
    public class SenseControlSlider : SenseControl
    {
        private int _height;
        public int Percent { get; set; }

        public SenseControlSlider()
        {
            ItemRectangle = new Rectangle();
            _height = -1;
            ItemType = XFItemType.Clickable;
        }

        public override int GetHeight()
        {
            if (_height > -1)
                return _height;

            ItemRectangle = this.GetImageRec("slider-back");
            _height = ItemRectangle.Height;
            return _height;
        }

        public override void ResetHeight()
        {
            _height = -1;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            DrawUI("slider-back", g, new Rectangle(x, y, ItemRectangle.Width, ItemRectangle.Height));

            Rectangle point = this.GetImageRec("slider-point");
            int pointX = (int)((Percent / (double)100) * (ItemRectangle.Width - point.Width));

            DrawUI("slider-green", g, new Rectangle(x + (point.Width >> 1), y, pointX, ItemRectangle.Height), false);
            DrawUI("slider-point", g, new Rectangle(x + pointX, y, point.Width, point.Height));
        }

        public override void OnMouseMove(int x, int y)
        {
            base.OnMouseMove(x, y);
            if (x > 0 && x < ItemRectangle.Width)
            {
                Percent = (x * 100) / ItemRectangle.Width;
            }
            else if (x < 0)
                Percent = 0;
            else
                Percent = 100;
        }

        internal bool OnDot(int localX, int localY)
        {
            Rectangle point = this.GetImageRec("slider-point");
            int pointX = (int)((Percent / (double)100) * (ItemRectangle.Width - point.Width));

            if (localY > 0 && Math.Abs(localX - pointX) < (Screen.PrimaryScreen.WorkingArea.Width / 12))
                return true;

            return false;
        }
    }
}
